﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscountCoupon.Phone.Common;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace DiscountCoupon
{
    /// <summary>
    /// 最新优惠控制器
    /// </summary>
    public partial class MainPage
    {
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void First_ListBox_Loaded(object sender, RoutedEventArgs e)
        {
            //从缓存中读取
            if (IsolatedStorageSettings.ApplicationSettings.Contains("CardList"))
            {
                var item = IsolatedStorageSettings.ApplicationSettings["CardList"] as IList<CouponItemCard>;
                if (item != null && item.Count > 0)
                {
                    App.ItemCard = item;
                    RetriveCardData();
                }
            }

            if (!App.IsCardListLoaed)
            {
                dwHelper.StartRequest("http://loveios.com/aspx/app/DC/card.aspx", UriKind.Absolute, "CardList", helper_onRequestCardListCompleted);
                this.loadingProgressBar.IsIndeterminate = true;
            }
            else
            {
                this.FirstListBoxNews.ItemsSource = App.ItemCard;
                RetriveCardData();
            }
            this.FirstListBoxNews.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(FirstListBoxNews_Tap);
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FirstListBoxNews_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {            
            ListBox lb = sender as ListBox;
            CouponItemCard cic=(lb.SelectedItem as CouponItemCard);
            if (cic == null)
                return;
            NavigationService.Navigate(new Uri("/CardView.xaml?id=" + cic.CardId, UriKind.Relative));
        }        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void helper_onRequestCardListCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DataContractJsonSerializer serrilizer = new DataContractJsonSerializer(typeof(List<CouponItemCard>));
            object obj;
            obj = serrilizer.ReadObject(e.Result);
            App.ItemCard = obj as IList<CouponItemCard>;
            IsolatedStorageSettings.ApplicationSettings["CardList"] = App.ItemCard;
            //this.FirstListBoxNews.ItemsSource = itemCard;   
            RetriveCardData();
            App.IsCardListLoaed = true;
        }

        /// <summary>
        /// 过滤不需要显示的优惠卡
        /// </summary>
        private void RetriveCardData()
        {
            int[] ids = GetSettingSeller();
            var data = App.ItemCard.Where(o => !ids.Contains(o.Seller));
            this.FirstListBoxNews.ItemsSource = data;
            if (data.Count() > 0)
                TB_NoCard.Visibility = Visibility.Collapsed;
            else
                TB_NoCard.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// 取ITEM项的缓存KEY
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        private string GetFileKey(CouponItemCard card)
        {
            string key = card.CardId.ToString().ToUpper() + System.IO.Path.GetExtension(card.CardUrl);
            return key;
        }

    }
}
